//
//  NWMainViewController.swift
//  Weibo
//
//  Created by Northwan on 16/1/19.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit

class NWMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //tabar是一个系统的属性，只允许读，不允许写，所以使用kvc替换为自己的button
        let tabBar = NWTabBar()
        
        setValue(tabBar, forKeyPath: "tabBar")
        
        tabBar.composeButtonClosure = {[weak self] in
            printLog("撰写按钮点击")
            
            //如果直接调用self会造成循环引用,所以要用[weak self]修饰一下，但是在调用self时，就会一个可选(self?)
            printLog(self?.view)
        }

        addChildViewController(NWHomeTableViewController(),imgName: "tabbar_home",title: "首页");
        addChildViewController(NWMessageTableViewController(),imgName: "tabbar_message_center",title: "消息");
        addChildViewController(NWDiscoverTableViewController(),imgName: "tabbar_discover",title: "发现");
        addChildViewController(NWProfileTableViewController(),imgName: "tabbar_profile",title: "我");
    }
    
    /**
     添加子控制器，重写系统的方法
     
     - parameter childController: 要添加的控制器对象tabBar图片
     - parameter imgName:         tabBar图片
     - parameter title:           tabBar名字
     */
    func addChildViewController(childController: UIViewController,imgName: String,title: String) {
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imgName)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(12)], forState: .Normal)
        childController.tabBarItem.imageInsets = UIEdgeInsets(top: -2, left: 0, bottom: 2, right: 0)
        addChildViewController(NWNavigationController(rootViewController: childController))
    }
}
